home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / me39src2.arc / BIND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-17  |  16.1 KB  |  717 lines

  1. /*    This file is for functions having to do with key bindings,
  2.     descriptions, help commands and startup file.
  3.  
  4.     written 11-feb-86 by Daniel Lawrence
  5.                                 */
  6.  
  7. #include    <stdio.h>
  8. #include    "estruct.h"
  9. #include    "edef.h"
  10. #include    "epath.h"
  11.  
  12. extern int meta(), cex(), unarg(), ctrlg(); /* dummy prefix binding functions */
  13.  
  14. help(f, n)    /* give me some help!!!!
  15.            bring up a fake buffer and read the help file
  16.            into it with view mode            */
  17. {
  18.     register WINDOW *wp;    /* scaning pointer to windows */
  19.     register BUFFER *bp;    /* buffer pointer to help */
  20.     char *fname;        /* ptr to file returned by flook() */
  21.  
  22.     /* first check if we are already here */
  23.     bp = bfind("emacs.hlp", FALSE, BFINVS);
  24.  
  25.     if (bp == NULL) {
  26.         fname = flook(pathname[1], FALSE);
  27.         if (fname == NULL) {
  28.             mlwrite("[Help file is not online]");
  29.             return(FALSE);
  30.         }
  31.     }
  32.  
  33.     /* split the current window to make room for the help stuff */
  34.     if (splitwind(FALSE, 1) == FALSE)
  35.             return(FALSE);
  36.  
  37.     if (bp == NULL) {
  38.         /* and read the stuff in */
  39.         if (getfile(fname, FALSE) == FALSE)
  40.             return(FALSE);
  41.     } else
  42.         swbuffer(bp);
  43.  
  44.     /* make this window in VIEW mode, update all mode lines */
  45.     curwp->w_bufp->b_mode |= MDVIEW;
  46.     curwp->w_bufp->b_flag |= BFINVS;
  47.     wp = wheadp;
  48.     while (wp != NULL) {
  49.         wp->w_flag |= WFMODE;
  50.         wp = wp->w_wndp;
  51.     }
  52.     return(TRUE);
  53. }
  54.  
  55. deskey(f, n)    /* describe the command for a certain key */
  56.  
  57. {
  58.     register int c;        /* key to describe */
  59.     register char *ptr;    /* string pointer to scan output strings */
  60.     char outseq[NSTRING];    /* output buffer for command sequence */
  61.     int (*getbind())();
  62.  
  63.     /* prompt the user to type us a key to describe */
  64.     mlwrite(": describe-key ");
  65.  
  66.     /* get the command sequence to describe
  67.        change it to something we can print as well */
  68.     cmdstr(c = getckey(FALSE), &outseq[0]);
  69.  
  70.     /* and dump it out */
  71.     ostring(outseq);
  72.     ostring(" ");
  73.  
  74.     /* find the right ->function */
  75.     if ((ptr = getfname(getbind(c))) == NULL)
  76.         ptr = "Not Bound";
  77.  
  78.     /* output the command sequence */
  79.     ostring(ptr);
  80. }
  81.  
  82. /* bindtokey:    add a new key to the key binding table        */
  83.  
  84. bindtokey(f, n)
  85.  
  86. int f, n;    /* command arguments [IGNORED] */
  87.  
  88. {
  89.     register unsigned int c;/* command key to bind */
  90.     register (*kfunc)();    /* ptr to the requested function to bind to */
  91.     register KEYTAB *ktp;    /* pointer into the command table */
  92.     register int found;    /* matched command flag */
  93.     char outseq[80];    /* output buffer for keystroke sequence */
  94.     int (*getname())();
  95.  
  96.     /* prompt the user to type in a key to bind */
  97.     mlwrite(": bind-to-key ");
  98.  
  99.     /* get the function name to bind it to */
  100.     kfunc = getname();
  101.     if (kfunc == NULL) {
  102.         mlwrite("[No such function]");
  103.         return(FALSE);
  104.     }
  105.     ostring(" ");
  106.  
  107.     /* get the command sequence to bind */
  108.     c = getckey((kfunc == meta) || (kfunc == cex) ||
  109.                 (kfunc == unarg) || (kfunc == ctrlg));
  110.  
  111.     /* change it to something we can print as well */
  112.     cmdstr(c, &outseq[0]);
  113.  
  114.     /* and dump it out */
  115.     ostring(outseq);
  116.  
  117.     /* if the function is a prefix key */
  118.     if (kfunc == meta || kfunc == cex ||
  119.         kfunc == unarg || kfunc == ctrlg) {
  120.  
  121.         /* search for an existing binding for the prefix key */
  122.         ktp = &keytab[0];
  123.         found = FALSE;
  124.         while (ktp->k_fp != NULL) {
  125.             if (ktp->k_fp == kfunc)
  126.                 unbindchar(ktp->k_code);
  127.             ++ktp;
  128.         }
  129.  
  130.         /* reset the appropriate global prefix variable */
  131.         if (kfunc == meta)
  132.             metac = c;
  133.         if (kfunc == cex)
  134.             ctlxc = c;
  135.         if (kfunc == unarg)
  136.             reptc = c;
  137.         if (kfunc == ctrlg)
  138.             abortc = c;
  139.     }
  140.  
  141.     /* search the table to see if it exists */
  142.     ktp = &keytab[0];
  143.     found = FALSE;
  144.     while (ktp->k_fp != NULL) {
  145.         if (ktp->k_code == c) {
  146.             found = TRUE;
  147.             break;
  148.         }
  149.         ++ktp;
  150.     }
  151.  
  152.     if (found) {    /* it exists, just change it then */
  153.         ktp->k_fp = kfunc;
  154.     } else {    /* otherwise we need to add it to the end */
  155.         /* if we run out of binding room, bitch */
  156.         if (ktp >= &keytab[NBINDS]) {
  157.             mlwrite("Binding table FULL!");
  158.             return(FALSE);
  159.         }
  160.  
  161.         ktp->k_code = c;    /* add keycode */
  162.         ktp->k_fp = kfunc;    /* and the function pointer */
  163.         ++ktp;            /* and make sure the next is null */
  164.         ktp->k_code = 0;
  165.         ktp->k_fp = NULL;
  166.     }
  167.     return(TRUE);
  168. }
  169.  
  170. /* unbindkey:    delete a key from the key binding table    */
  171.  
  172. unbindkey(f, n)
  173.  
  174. int f, n;    /* command arguments [IGNORED] */
  175.  
  176. {
  177.     register int c;        /* command key to unbind */
  178.     char outseq[80];    /* output buffer for keystroke sequence */
  179.  
  180.     /* prompt the user to type in a key to unbind */
  181.     mlwrite(": unbind-key ");
  182.  
  183.     /* get the command sequence to unbind */
  184.     c = getckey(FALSE);        /* get a command sequence */
  185.  
  186.     /* change it to something we can print as well */
  187.     cmdstr(c, &outseq[0]);
  188.  
  189.     /* and dump it out */
  190.     ostring(outseq);
  191.  
  192.     /* if it isn't bound, bitch */
  193.     if (unbindchar(c) == FALSE) {
  194.         mlwrite("[Key not bound]");
  195.         return(FALSE);
  196.     }
  197.     return(TRUE);
  198. }
  199.  
  200. unbindchar(c)
  201.  
  202. int c;        /* command key to unbind */
  203.  
  204. {
  205.     register KEYTAB *ktp;    /* pointer into the command table */
  206.     register KEYTAB *sktp;    /* saved pointer into the command table */
  207.     register int found;    /* matched command flag */
  208.  
  209.     /* search the table to see if the key exists */
  210.     ktp = &keytab[0];
  211.     found = FALSE;
  212.     while (ktp->k_fp != NULL) {
  213.         if (ktp->k_code == c) {
  214.             found = TRUE;
  215.             break;
  216.         }
  217.         ++ktp;
  218.     }
  219.  
  220.     /* if it isn't bound, bitch */
  221.     if (!found)
  222.         return(FALSE);
  223.  
  224.     /* save the pointer and scan to the end of the table */
  225.     sktp = ktp;
  226.     while (ktp->k_fp != NULL)
  227.         ++ktp;
  228.     --ktp;        /* backup to the last legit entry */
  229.  
  230.     /* copy the last entry to the current one */
  231.     sktp->k_code = ktp->k_code;
  232.     sktp->k_fp   = ktp->k_fp;
  233.  
  234.     /* null out the last one */
  235.     ktp->k_code = 0;
  236.     ktp->k_fp = NULL;
  237.     return(TRUE);
  238. }
  239.  
  240. desbind(f, n)    /* describe bindings
  241.            bring up a fake buffer and list the key bindings
  242.            into it with view mode            */
  243.  
  244. #if    APROP
  245. {
  246.     buildlist(TRUE, "");
  247. }
  248.  
  249. apro(f, n)    /* Apropos (List functions that match a substring) */
  250.  
  251. {
  252.     char mstring[NSTRING];    /* string to match cmd names to */
  253.     int status;        /* status return */
  254.  
  255.     status = mlreply("Apropos string: ", mstring, NSTRING - 1);
  256.     if (status != TRUE)
  257.         return(status);
  258.  
  259.     return(buildlist(FALSE, mstring));
  260. }
  261.  
  262. buildlist(type, mstring)  /* build a binding list (limited or full) */
  263.  
  264. int type;    /* true = full list,   false = partial list */
  265. char *mstring;    /* match string if a partial list */
  266.  
  267. #endif
  268. {
  269. #if    ST520 & LATTICE
  270. #define    register        
  271. #endif
  272.     register WINDOW *wp;    /* scanning pointer to windows */
  273.     register KEYTAB *ktp;    /* pointer into the command table */
  274.     register NBIND *nptr;    /* pointer into the name binding table */
  275.     register BUFFER *bp;    /* buffer to put binding list into */
  276.     char *strp;        /* pointer int string to send */
  277.     int cpos;        /* current position to use in outseq */
  278.     char outseq[80];    /* output buffer for keystroke sequence */
  279.  
  280.     /* split the current window to make room for the binding list */
  281.     if (splitwind(FALSE, 1) == FALSE)
  282.             return(FALSE);
  283.  
  284.     /* and get a buffer for it */
  285.     bp = bfind("Binding list", TRUE, 0);
  286.     if (bp == NULL || bclear(bp) == FALSE) {
  287.         mlwrite("Can not display binding list");
  288.         return(FALSE);
  289.     }
  290.  
  291.     /* let us know this is in progress */
  292.     mlwrite("[Building binding list]");
  293.  
  294.     /* disconect the current buffer */
  295.         if (--curbp->b_nwnd == 0) {             /* Last use.            */
  296.                 curbp->b_dotp  = curwp->w_dotp;
  297.                 curbp->b_doto  = curwp->w_doto;
  298.                 curbp->b_markp = curwp->w_markp;
  299.                 curbp->b_marko = curwp->w_marko;
  300.         }
  301.  
  302.     /* connect the current window to this buffer */
  303.     curbp = bp;    /* make this buffer current in current window */
  304.     bp->b_mode = 0;        /* no modes active in binding list */
  305.     bp->b_nwnd++;        /* mark us as more in use */
  306.     wp = curwp;
  307.     wp->w_bufp = bp;
  308.     wp->w_linep = bp->b_linep;
  309.     wp->w_flag = WFHARD|WFFORCE;
  310.     wp->w_dotp = bp->b_dotp;
  311.     wp->w_doto = bp->b_doto;
  312.     wp->w_markp = NULL;
  313.     wp->w_marko = 0;
  314.  
  315.     /* build the contents of this window, inserting it line by line */
  316.     nptr = &names[0];
  317.     while (nptr->n_func != NULL) {
  318.  
  319.         /* add in the command name */
  320.         strcpy(outseq, nptr->n_name);
  321.         cpos = strlen(outseq);
  322.         
  323. #if    APROP
  324.         /* if we are executing an apropos command..... */
  325.         if (type == FALSE &&
  326.             /* and current string doesn't include the search string */
  327.             strinc(outseq, mstring) == FALSE)
  328.             goto fail;
  329. #endif
  330.         /* search down any keys bound to this */
  331.         ktp = &keytab[0];
  332.         while (ktp->k_fp != NULL) {
  333.             if (ktp->k_fp == nptr->n_func) {
  334.                 /* padd out some spaces */
  335.                 while (cpos < 25)
  336.                     outseq[cpos++] = ' ';
  337.  
  338.                 /* add in the command sequence */
  339.                 cmdstr(ktp->k_code, &outseq[cpos]);
  340.                 while (outseq[cpos] != 0)
  341.                     ++cpos;
  342.  
  343.                 /* and add it as a line into the buffer */
  344.                 strp = &outseq[0];
  345.                 while (*strp != 0)
  346.                     linsert(1, *strp++);
  347.                 lnewline();
  348.  
  349.                 cpos = 0;    /* and clear the line */
  350.             }
  351.             ++ktp;
  352.         }
  353.  
  354.         /* if no key was bound, we need to dump it anyway */
  355.         if (cpos > 0) {
  356.             outseq[cpos] = 0;
  357.             strp = &outseq[0];
  358.             while (*strp != 0)
  359.                 linsert(1, *strp++);
  360.             lnewline();
  361.         }
  362.  
  363. fail:        /* and on to the next name */
  364.         ++nptr;
  365.     }
  366.  
  367.     curwp->w_bufp->b_mode |= MDVIEW;/* put this buffer view mode */
  368.     curbp->b_flag &= ~BFCHG;    /* don't flag this as a change */
  369.     wp->w_dotp = lforw(bp->b_linep);/* back to the beginning */
  370.     wp->w_doto = 0;
  371.     wp = wheadp;            /* and update ALL mode lines */
  372.     while (wp != NULL) {
  373.         wp->w_flag |= WFMODE;
  374.         wp = wp->w_wndp;
  375.     }
  376.     mlwrite("");    /* clear the mode line */
  377.     return(TRUE);
  378. }
  379.  
  380. #if    APROP
  381. strinc(source, sub)    /* does source include sub? */
  382.  
  383. char *source;    /* string to search in */
  384. char *sub;    /* substring to look for */
  385.  
  386. {
  387.     char *sp;    /* ptr into source */
  388.     char *nxtsp;    /* next ptr into source */
  389.     char *tp;    /* ptr into substring */
  390.  
  391.     /* for each character in the source string */
  392.     sp = source;
  393.     while (*sp) {
  394.         tp = sub;
  395.         nxtsp = sp;
  396.  
  397.         /* is the substring here? */
  398.         while (*tp) {
  399.             if (*nxtsp++ != *tp)
  400.                 break;
  401.             else
  402.                 tp++;
  403.         }
  404.  
  405.         /* yes, return a success */
  406.         if (*tp == 0)
  407.             return(TRUE);
  408.  
  409.         /* no, onward */
  410.         sp++;
  411.     }
  412.     return(FALSE);
  413. }
  414. #endif
  415.  
  416. /* get a command key sequence from the keyboard    */
  417.  
  418. unsigned int getckey(mflag)
  419.  
  420. int mflag;    /* going for a meta sequence? */
  421.  
  422. {
  423.     register unsigned int c;    /* character fetched */
  424. #if    MSC | TURBO
  425.     register unsigned char *tp;    /* pointer into the token */
  426. #else
  427.     register char *tp;        /* pointer into the token */
  428. #endif
  429.     char tok[NSTRING];        /* command incoming */
  430.  
  431.     /* check to see if we are executing a command line */
  432.     if (clexec) {
  433.         macarg(tok);    /* get the next token */
  434.         return(stock(tok));
  435.     }
  436.  
  437.     /* or the normal way */
  438.     if (mflag)
  439.         c = get1key();
  440.     else
  441.         c = getcmd();
  442.     return(c);
  443. }
  444.  
  445. /* execute the startup file */
  446.  
  447. startup(sfname)
  448.  
  449. char *sfname;    /* name of startup file (null if default) */
  450.  
  451. {
  452.     char *fname;    /* resulting file name to execute */
  453.  
  454.     /* look up the startup file */
  455.     if (*sfname != 0)
  456.         fname = flook(sfname, TRUE);
  457.     else
  458.         fname = flook(pathname[0], TRUE);
  459.  
  460.     /* if it isn't around, don't sweat it */
  461.     if (fname == NULL)
  462.         return(TRUE);
  463.  
  464.     /* otherwise, execute the sucker */
  465.     return(dofile(fname));
  466. }
  467.  
  468. /*    Look up the existance of a file along the normal or PATH
  469.     environment variable. Look first in the HOME directory if
  470.     asked and possible
  471. */
  472.  
  473. char *flook(fname, hflag)
  474.  
  475. char *fname;    /* base file name to search for */
  476. int hflag;    /* Look in the HOME environment variable first? */
  477.  
  478. {
  479.     register char *home;    /* path to home directory */
  480.     register char *path;    /* environmental PATH variable */
  481.     register char *sp;    /* pointer into path spec */
  482.     register int i;        /* index */
  483.     register int status;    /* return status */
  484.     static char fspec[NSTRING];    /* full path spec to search */
  485.     char *getenv();
  486.  
  487. #if    ENVFUNC
  488.  
  489.     if (hflag) {
  490.         home = getenv("HOME");
  491.         if (home != NULL) {
  492.             /* build home dir file spec */
  493.             strcpy(fspec, home);
  494.             strcat(fspec, "/");
  495.             strcat(fspec, fname);
  496.  
  497.             /* and try it out */
  498.             if (ffropen(fspec) == FIOSUC) {
  499.                 ffclose();
  500.                 return(fspec);
  501.             }
  502.         }
  503.     }
  504. #endif
  505.  
  506.     /* always try the current directory first */
  507.     if (ffropen(fname) == FIOSUC) {
  508.         ffclose();
  509.         return(fname);
  510.     }
  511.  
  512. #if    ENVFUNC
  513.     /* get the PATH variable */
  514.     path = getenv("PATH");
  515.     if (path != NULL)
  516.         while (*path) {
  517.  
  518.             /* build next possible file spec */
  519.             sp = fspec;
  520.             while (*path && (*path != PATHCHR))
  521.                 *sp++ = *path++;
  522.             *sp++ = '/';
  523.             *sp = 0;
  524.             strcat(fspec, fname);
  525.  
  526.             /* and try it out */
  527.             if (ffropen(fspec) == FIOSUC) {
  528.                 ffclose();
  529.                 return(fspec);
  530.             }
  531.  
  532.             if (*path == PATHCHR)
  533.                 ++path;
  534.         }
  535. #endif
  536.  
  537.     /* look it up via the old table method */
  538.     for (i=2; i < NPNAMES; i++) {
  539.         strcpy(fspec, pathname[i]);
  540.         strcat(fspec, fname);
  541.  
  542.         /* and try it out */
  543.         if (ffropen(fspec) == FIOSUC) {
  544.             ffclose();
  545.             return(fspec);
  546.         }
  547.     }
  548.  
  549.     return(NULL);    /* no such luck */
  550. }
  551.  
  552. cmdstr(c, seq)    /* change a key command to a string we can print out */
  553.  
  554. int c;        /* sequence to translate */
  555. char *seq;    /* destination string for sequence */
  556.  
  557. {
  558.     char *ptr;    /* pointer into current position in sequence */
  559.  
  560.     ptr = seq;
  561.  
  562.     /* apply meta sequence if needed */
  563.     if (c & META) {
  564.         *ptr++ = 'M';
  565.         *ptr++ = '-';
  566.     }
  567.  
  568.     /* apply ^X sequence if needed */
  569.     if (c & CTLX) {
  570.         *ptr++ = '^';
  571.         *ptr++ = 'X';
  572.     }
  573.  
  574.     /* apply SPEC sequence if needed */
  575.     if (c & SPEC) {
  576.         *ptr++ = 'F';
  577.         *ptr++ = 'N';
  578.     }
  579.  
  580.     /* apply control sequence if needed */
  581.     if (c & CTRL) {
  582.         *ptr++ = '^';
  583.     }
  584.  
  585.     c = c & 255;    /* strip the prefixes */
  586.  
  587.     /* and output the final sequence */
  588.  
  589.     *ptr++ = c;
  590.     *ptr = 0;    /* terminate the string */
  591. }
  592.  
  593. /*    This function looks a key binding up in the binding table    */
  594.  
  595. int (*getbind(c))()
  596.  
  597. int c;    /* key to find what is bound to it */
  598.  
  599. {
  600.     register KEYTAB *ktp;
  601.  
  602.         ktp = &keytab[0];                       /* Look in key table.   */
  603.         while (ktp->k_fp != NULL) {
  604.                 if (ktp->k_code == c)
  605.                         return(ktp->k_fp);
  606.                 ++ktp;
  607.         }
  608.  
  609.     /* no such binding */
  610.     return(NULL);
  611. }
  612.  
  613. /* getfname:    This function takes a ptr to function and gets the name
  614.         associated with it
  615. */
  616.  
  617. char *getfname(func)
  618.  
  619. int (*func)();    /* ptr to the requested function to bind to */
  620.  
  621. {
  622.     register NBIND *nptr;    /* pointer into the name binding table */
  623.  
  624.     /* skim through the table, looking for a match */
  625.     nptr = &names[0];
  626.     while (nptr->n_func != NULL) {
  627.         if (nptr->n_func == func)
  628.             return(nptr->n_name);
  629.         ++nptr;
  630.     }
  631.     return(NULL);
  632. }
  633.  
  634. int (*fncmatch(fname))() /* match fname to a function in the names table
  635.                 and return any match or NULL if none        */
  636.  
  637. char *fname;    /* name to attempt to match */
  638.  
  639. {
  640.     register NBIND *ffp;    /* pointer to entry in name binding table */
  641.  
  642.     /* scan through the table, returning any match */
  643.     ffp = &names[0];
  644.     while (ffp->n_func != NULL) {
  645.         if (strcmp(fname, ffp->n_name) == 0)
  646.             return(ffp->n_func);
  647.         ++ffp;
  648.     }
  649.     return(NULL);
  650. }
  651.  
  652. /* stock:    String key name TO Command Key        */
  653.  
  654. unsigned int stock(keyname)
  655.  
  656. char *keyname;        /* name of key to translate to Command key form */
  657.  
  658. {
  659.     register unsigned int c;    /* key sequence to return */
  660.  
  661.     /* parse it up */
  662.     c = 0;
  663.  
  664.     /* first, the META prefix */
  665.     if (*keyname == 'M' && *(keyname+1) == '-') {
  666.         c = META;
  667.         keyname += 2;
  668.     }
  669.  
  670.     /* next the function prefix */
  671.     if (*keyname == 'F' && *(keyname+1) == 'N') {
  672.         c |= SPEC;
  673.         keyname += 2;
  674.     }
  675.  
  676.     /* control-x as well... (but not with FN) */
  677.     if (*keyname == '^' && *(keyname+1) == 'X'&& !(c & SPEC)) {
  678.         c |= CTLX;
  679.         keyname += 2;
  680.     }
  681.  
  682.     /* a control char? */
  683.     if (*keyname == '^' && *(keyname+1) != 0) {
  684.         c |= CTRL;
  685.         ++keyname;
  686.     }
  687.     if (*keyname < 32) {
  688.         c |= CTRL;
  689.         *keyname += 'A';
  690.     }
  691.  
  692.  
  693.     /* make sure we are not lower case (not with function keys)*/
  694.     if (*keyname >= 'a' && *keyname <= 'z' && !(*keyname & SPEC))
  695.         *keyname -= 32;
  696.  
  697.     /* the final sequence... */
  698.     c |= *keyname;
  699.     return(c);
  700. }
  701.  
  702. char *transbind(skey)    /* string key name to binding name.... */
  703.  
  704. char *skey;    /* name of keey to get binding for */
  705.  
  706. {
  707.     char *bindname;
  708.     unsigned int stock();
  709.     int (*getbind())();
  710.  
  711.     bindname = getfname(getbind(stock(skey)));
  712.     if (bindname == NULL)
  713.         bindname = "ERROR";
  714.  
  715.     return(bindname);
  716. }
  717.